//
//  BozhuMainViewController.m
//  Ting
//
//  Created by 安静的美男子 on 14/11/25.
//  Copyright (c) 2014年 安静的美男子. All rights reserved.
//

#import "BozhuMainViewController.h"

@interface BozhuMainViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation BozhuMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    self.TopData = [NSMutableArray array];
    self.DownData = [NSMutableArray array];
    
    
    [self CreatTable];
    
    
    [self getData];
    
    self.mbview = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbview.labelText = @"正在努力加载中...";
    [self.mbview setMode:MBProgressHUDModeIndeterminate];
    [self.mbview show:YES];
    [self.view addSubview:self.mbview];
    

    [self CreatTabbar];
    
    
    // Do any additional setup after loading the view.
}

-(void)getData
{
    
    GetData *data = [[GetData alloc]init];
    [data getDataURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?device=iPhone&toUid=%@",_Uid] dic:nil block:^(id result) {
        BozhuMainModel *model = [[BozhuMainModel alloc]init];
        [model setValuesForKeysWithDictionary:result];
        [_TopData addObject:model];
        [_table reloadData];

    }];
    [data getDataURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/%@/1/2?device=iPhone",_Uid] dic:nil block:^(id result) {
        NSArray *arr = [result objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            
            BozhuAlbumModel *model = [[BozhuAlbumModel alloc]init];
           [ model setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:model];
        }
    
        [_table reloadData];

        
        
    }];
    [data getDataURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/1/30?device=iPhone",_Uid] dic:nil block:^(id result) {
        
        NSArray *arr = [result objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            
            SortingModel *model = [[SortingModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_DownData addObject:model];
            
        }
        [_table reloadData];

        
        
    }];
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        
        return 150;
    }else
    {
        return 100;
    }
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return [NSString stringWithFormat:@"推荐专辑(%lu)",(unsigned long)[_dataSource count]];
        
    }
    else
        return [NSString stringWithFormat:@"推荐声音(%lu)",(unsigned long)_DownData.count];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        PlayViewController *vc = [[PlayViewController alloc]init];
        vc.num = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        vc.DataSource = _DownData;
        vc.model = _DownData[indexPath.row];
        [self presentViewController:vc animated:YES completion:nil];
        
    }
    else
    {
        BoZhuViewController *vc = [[BoZhuViewController alloc]init];
        vc.MyId = [_dataSource[indexPath.row] albumId];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
    
    
    static NSString *identifier = @"sort";
    
    SortingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SortingTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
        [cell setModel:_DownData[indexPath.row]];
        
    
    
    return cell;
    
}else
{
    static NSString *alb = @"album";
    
    AlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:alb];
    if (!cell) {
        cell = [[AlbumTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:alb];
    }
    [cell setModel:_dataSource[indexPath.row]];
    
    return cell;
    
}

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (_dataSource.count != 0 && _TopData.count != 0 &&_DownData.count != 0) {
        
        [self.mbview removeFromSuperview];

    
    if (section == 0) {
        return _dataSource.count;
    }
    if (section == 1) {
        
        return _DownData.count;
    }
    }
   
    return 0;
    
}
-(void)CreatTable
{
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568 - 44) style:UITableViewStyleGrouped];
    [self.view addSubview:_table];
    _table.delegate = self;
    _table.dataSource = self;
    
}
-(void)CreatTabbar
{
    UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(-1, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width + 2, 51)];
    downView.backgroundColor = [UIColor blackColor];
    downView.alpha = 1;
    downView.layer.borderWidth = 1;
    downView.layer.borderColor =[[UIColor grayColor ]CGColor];;
    
    [self.view addSubview:downView];
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 15, 24, 24);
    
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [downView addSubview:backButton];
    
    
    
    
    UIView *black = [[UIView alloc]initWithFrame:CGRectMake(64 * 2, 568 - 64, 64, 64)];
    [self.view addSubview:black];
    black.layer.cornerRadius = 32;
    black.clipsToBounds = YES;
    
    black.backgroundColor = [UIColor blackColor];
    
    self.play = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.play.frame = CGRectMake(0, 0, 60, 60);
    self.play.center = black.center;
    
    [self.view addSubview:self.play];
    
    self.play.layer.cornerRadius = 30;
    [self.view bringSubviewToFront:self.play];
    self.play.clipsToBounds = YES;
    [self.play addTarget:self action:@selector(GotoPlay) forControlEvents:UIControlEventTouchUpInside];
    
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    
    
    
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    _Playmodel = audioManager.Mymodel;
    
    
    if (audioManager.flag) {
        
        if (!_Timer) {
            //            self.Timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
            
            self.Timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
        }
        
        //将定时器加入循环
        //循环用于系统页面响应,定时器和进程
        //                [[NSRunLoop currentRunLoop] addTimer:_Timer forMode:NSDefaultRunLoopMode];
        
        
        [_Timer setFireDate:[NSDate distantPast]];
        
        
        
        
    }
    else
    {
        
        [_Timer setFireDate:[NSDate distantFuture]];
        
        //        if ([_Timer isValid]) {
        //            永久停止
        //            [self.Timer invalidate];
        //            _Timer =nil;
        //        }
        
        
    }
    
}
-(void)zhuan
{
    self.play.transform = CGAffineTransformRotate(self.play.transform,2 * M_PI / 60.0 /100);
    //    if (_model.coverLarge) {
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    //    }
}

-(void)GotoPlay
{
    PlayViewController *vc = [[PlayViewController alloc]init];
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    vc.model = audioManager.Mymodel;
    vc.DataSource = audioManager.Mydata;
    /**
     *  number不记录
     */
    
    //    vc.number = audioManager.num;
    vc.num = [NSString stringWithFormat:@"%ld",audioManager.num];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [self.Timer invalidate];
    _Timer =nil;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
